package com.select;

import com.google.gson.Gson;
import com.util.purchaseUtil;
import com.bean.purchase.purchaseBean;
import com.bean.supplier.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/purchaseServlet")
public class purchaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         String type = req.getParameter("type");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset:utf-8");
        resp.setCharacterEncoding("utf-8");
        /**
         * 向数据库添加数据
         */
        if("1".equals(type)){
            try {
               String userId = req.getSession().getAttribute("uid").toString();//业务员id
               String name = req.getParameter("commodityName");//商品名称
               String cas = req.getParameter("cas");//cas号
               String amount =  req.getParameter("amount");//数量
               String price =  req.getParameter("price");//单价
               String priceStatus = req.getParameter("priceStatus");//是否含税
               Double sumPrice =Double.valueOf(amount) * Double.valueOf(price);//总价
               String status = "0";//收货状态
               String userName = "";//业务员姓名

                purchaseBean purchasebean = purchaseUtil.selectUserName(userId);
                userName = purchasebean.getUser_name();

               String supplierName =  req.getParameter("supplierName");//供货商名称
               String supplierPhone = req.getParameter("supplierPhone");//联系电话
               String trackingNumber = "";//物流单号
               Date date = new Date();
               SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
               String create_time = df.format(date);//创建日期
               String ann = "1";//是否收到发票

                purchaseUtil.insert(userId,name,cas,amount,price,priceStatus,sumPrice,status,userName,supplierName,supplierPhone,trackingNumber,create_time,ann);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 查询所有供应商的姓名
         */
        if("2".equals(type)){
             try {
                 List<supplierBean> list = purchaseUtil.selectSupplierName();
                 System.out.println(list.get(0));
                 resp.getWriter().write(new Gson().toJson(list));
             } catch (SQLException e) {
                 e.printStackTrace();
             }
         }
        /**
         * 根据供应商id查询供应商电话
         */
        if("3".equals(type)){
             String gid = req.getParameter("gid");
            try {
             List<supplierBean> list = purchaseUtil.selectSupplierPhone(gid);
             resp.getWriter().write(new Gson().toJson(list));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 模糊查询供应商
         */
        if("4".equals(type)){

            String name = req.getParameter("supplierName");
            try {
                List<supplierBean> list = purchaseUtil.dimSelectSupplierName(name);
                resp.getWriter().write(new Gson().toJson(list));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 查询采购订单全部数据
         */
        if("5".equals(type)){
            try {
                List<purchaseBean> list = purchaseUtil.selectAll();
                resp.getWriter().write(new Gson().toJson(list));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 查询全部行数 得到全部页数
         */
        if("10".equals(type)){
            try {
                String name = req.getParameter("commodityName");//商品名
                String cas =  req.getParameter("cas");//cas号
                String supplierName =  req.getParameter("supplierName");//供应商姓名
                String trackingNumber =  req.getParameter("trackingNumber");//物流编号
                purchaseBean purchase = purchaseUtil.selectLine(name,cas,supplierName,trackingNumber);
                Long line = purchase.getLine();
                Double d = Math.ceil(line*0.1);
                Integer page = d.intValue();
                purchase.setPage(page);
                resp.getWriter().write(new Gson().toJson(purchase));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 根据分页页数查询数据
         */
        if("11".equals(type)){
            String page = req.getParameter("page");
            Integer data = (Integer.valueOf(page)-1)*10;
            String name = req.getParameter("commodityName");//商品名
            String cas =  req.getParameter("cas");//cas号
            String supplierName =  req.getParameter("supplierName");//供应商姓名
            String trackingNumber =  req.getParameter("trackingNumber");//物流编号
            try {
                List<purchaseBean> list = purchaseUtil.selectPage(name,cas,supplierName,trackingNumber,data);
                resp.getWriter().write(new Gson().toJson(list));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 添加物流单号
         */
        if("12".equals(type)){
            String cid = req.getParameter("id");//采购管理
            String number = req.getParameter("number");//物流单号
            try {
                purchaseUtil.updateNumber(number,cid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 根据商品名字，cas号，物流单，供应商姓名
         */
        if("6".equals(type)){
          String name = req.getParameter("commodityName");//商品名
          String cas =  req.getParameter("cas");//cas号
          String supplierName =  req.getParameter("supplierName");//供应商姓名
          String trackingNumber =  req.getParameter("trackingNumber");//物流编号
            try {
                List<purchaseBean> list = purchaseUtil.selectPurchase(name,cas,supplierName,trackingNumber);
                resp.getWriter().write(new Gson().toJson(list));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 根据cid 删除一条数据
         */
        if("7".equals(type)){
            String cid = req.getParameter("radioValue");
            try {
                purchaseUtil.delete(cid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 查询采购管理中一条数据
         */
        if("8".equals(type)){
          String cid =  req.getParameter("cid");
            try {
                purchaseBean purchasebean = purchaseUtil.select(cid);
                resp.getWriter().write(new Gson().toJson(purchasebean));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if("9".equals(type)){
           String CommodityName = req.getParameter("CommodityName");//商品名
           String cas =  req.getParameter("cas");//cas号
           String amount = req.getParameter("amount");//商品数量
           String price = req.getParameter("price");//商品单价
           String priceStatus = req.getParameter("priceStatus");//是否含税
           String supplierName = req.getParameter("supplierName");//供应商名字
           String supplierPhone = req.getParameter("supplierPhone");//供应商电话
           String cid = req.getParameter("cid");
            try {
                purchaseUtil.update(CommodityName,cas,amount,price,priceStatus,supplierName,supplierPhone,Double.valueOf(amount) * Double.valueOf(price),cid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
